<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>新建租赁设备退场登记</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>退场单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="hidden" id="enginid" name="enginid">
                            <input type="text" readonly id="enginid_name" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>合同名称</label>
                        <div class="layui-input-block">
                            <input type="hidden" id="caiid" name="caiid">
                            <input type="text" readonly id="caiid_name" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"> 供应商</label>
                        <div class="layui-input-block">
                            <input type="text" id="gonger" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>登记时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="dt" readonly placeholder="请选择" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">登记人</label>
                        <div class="layui-input-block">
                            <input type="text" id="baner" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>



        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>退场明细</span></div>
                        <div>
                            <button type="button" onclick="choose_device_jin_list_page_devicetui()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从在场设备选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>设备名称及规格</th>
                            <th>设备编号</th>
                            <th>计数单位</th>
                            <th>进场日期</th>
                            <th>运行量</th>
                            <th><text>*</text>退场日期</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_jinid = getQueryVariable('jinid') || 0;


    function choose_device_jin_list_page_devicetui(obj){
        var caiid = $("#caiid").val();
        openPage('../public/choose_device_jin_list_page_devicetui.php?page=device_tui&caiid='+caiid,'80%','80%');
    }
    function choose_device_jin_list_page_devicetui_one(obj){
        var domkey = $(obj).parents('tr').index();
        var caiid = $("#caiid").val();
        openPage('../public/choose_device_jin_list_page_devicetui.php?choose_type=radio&page=device_tui_one&caiid='+caiid+'&domkey='+domkey,'80%','80%');
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_device_mingxi_tui, data:{jinid:param_jinid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#number").val(data.number);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);

                $("#enginid").val(data.engininfo.id)
                $("#enginid_name").val(data.engininfo.name)


                $("#caiid").val(data.caiidinfo.id)
                $("#caiid_name").val(data.caiidinfo.name)
                $("#gonger").val(data.caiidinfo.gonger)


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" onclick="choose_device_jin_list_page_devicetui_one(this)" placeholder="请选择" jinid="0" readonly class="layui-input name disabled">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" readonly class="layui-input shenum disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input digit disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input jindt disabled"></td>';
                listAddStr += '<td><input type="text" readonly class="layui-input yununit disabled"></td>';
                listAddStr += '<td><input type="text" class="layui-input tuidt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);

                if(data.list){
                    var str = '';
                    str += '<tr>';
                    str += '<td style="width: 45px">1</td>';
                    str += '<td>';
                    str += '<input type="text" onclick="choose_device_jin_list_page_devicetui_one(this)" placeholder="请选择" jinid="'+data.list.jinid+'" readonly class="layui-input name disabled" value="'+data.list.name+'">';
                    str += '</td>';
                    str += '<td><input type="text" readonly class="layui-input shenum disabled" value="'+data.list.shenum+'"></td>';
                    str += '<td><input type="text" readonly class="layui-input digit disabled" value="'+data.list.digit+'"></td>';
                    str += '<td><input type="text" readonly class="layui-input jindt disabled" value="'+data.list.jindt+'"></td>';
                    str += '<td><input type="text" readonly class="layui-input yununit disabled" value="'+data.list.yununit+'"></td>';
                    str += '<td><input type="text" class="layui-input tuidt bdate_ymd" readonly></td>';
                    str += '<td><input type="text" class="layui-input explain"></td>';
                    str += '</tr>';
                    $("#list_list").html(str);
                }

                update_refresh_row_func();
                render_from();
            }
        });



        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();


            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var jinid = $(this).find('.name').attr('jinid');
                    var tuidt = $(this).find('.tuidt').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!jinid){
                        throw new Error('第'+index+'行，请选择设备名称及规格');
                    }
                    if(!tuidt){
                        throw new Error('第'+index+'行，请选择退场日期');
                    }


                    list.push({
                        jinid:jinid,
                        tuidt:tuidt,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;



            data = parseMoney(data);
            facade.ajax({url: ask_device_mingxi_tui, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });


    function update_refresh_row_func() {
        render_date();
        render_from();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }



</script>
</body>
</html>
